package com.mxpio.erp.equipment.controller;

import com.mxpio.erp.common.equipment.entity.EquipmentPersonGroupDetail;
import com.mxpio.erp.common.equipment.service.EquipmentPersonGroupDetailService;
import com.mxpioframework.common.vo.Result;
import com.mxpioframework.jpa.policy.impl.SmartCrudPolicyAdapter;
import com.mxpioframework.jpa.query.Criteria;
import com.mxpioframework.jpa.query.Operator;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.web.bind.annotation.*;

import java.io.UnsupportedEncodingException;
import java.util.List;


@Tag(name = "EquipmentPersonGroupDetailController}", description = "人员群组管理明细")
@RestController
@RequestMapping("/erp/equipment/group/detail/")
public class EquipmentPersonGroupDetailController {

    @Autowired
    private EquipmentPersonGroupDetailService equipmentPersonGroupDetailService;


    @GetMapping("list")
    @Operation(summary = "人员群组管理明细列表", description = "获取人员群组管理明细列表（无分页）", method = "GET")
    public Result<List<EquipmentPersonGroupDetail>> list(@Parameter(description="查询参数") Criteria criteria) throws UnsupportedEncodingException {
        List<EquipmentPersonGroupDetail> list = equipmentPersonGroupDetailService.list(EquipmentPersonGroupDetail.class, criteria);
        return Result.OK(list);
    }

    @GetMapping("page")
    @Operation(summary = "人员群组管理明细列表", description = "获取人员群组管理明细列表（分页）", method = "GET")
    public Result<Page<EquipmentPersonGroupDetail>> page(@Parameter(description="查询参数") Criteria criteria, @Parameter(description="每页条数")Integer pageSize, @Parameter(description="页号")Integer pageNo,@Parameter(description = "群组id")String groupId) throws UnsupportedEncodingException {
        Pageable page = PageRequest.of(pageNo-1, pageSize);
        if (StringUtils.isNotBlank(groupId)) {
            criteria = criteria.addCriterion("groupId", Operator.EQ, groupId);
        }
        Page<EquipmentPersonGroupDetail> pageResult = equipmentPersonGroupDetailService.listPage(EquipmentPersonGroupDetail.class, page, criteria);
        return Result.OK(pageResult);
    }

    @GetMapping("list/{groupId}")
    @Operation(summary = "根据ID获取人员群组管理明细列表", description = "根据ID获取人员群组管理明细列表", method = "GET")
    public Result<List<EquipmentPersonGroupDetail>> getById(@PathVariable(name = "groupId", required = true) String groupId) {
        Criteria c = Criteria.create().addCriterion("groupId", Operator.EQ, groupId);
        List<EquipmentPersonGroupDetail> list = equipmentPersonGroupDetailService.list(EquipmentPersonGroupDetail.class, c);
        return Result.OK(list);
    }

    @GetMapping("listByCode/{groupCode}")
    @Operation(summary = "根据groupCode获取人员群组管理明细列表", description = "根据groupCode获取人员群组管理明细列表", method = "GET")
    public Result<List<EquipmentPersonGroupDetail>> getByCode(@PathVariable(name = "groupCode", required = true) String groupCode) {
        List<EquipmentPersonGroupDetail> list = equipmentPersonGroupDetailService.listByCode(groupCode);
        return Result.OK(list);
    }

    @RequestMapping("add")
    @Operation(summary = "保存人员群组管理明细", description = "保存人员群组管理明细")
    public Result<EquipmentPersonGroupDetail> add(@RequestBody EquipmentPersonGroupDetail equipmentPersonGroupDetail) {
            equipmentPersonGroupDetailService.save(equipmentPersonGroupDetail,new SmartCrudPolicyAdapter(){
        });
        return Result.OK(equipmentPersonGroupDetail);
    }

    @PutMapping("edit")
    @Operation(summary = "编辑人员群组管理明细", description = "编辑人员群组管理明细", method = "PUT")
    public Result<EquipmentPersonGroupDetail> edit(@RequestBody EquipmentPersonGroupDetail equipmentPersonGroupDetail) {
        equipmentPersonGroupDetailService.update(equipmentPersonGroupDetail);
        return Result.OK(equipmentPersonGroupDetail);
    }

    @DeleteMapping("remove/{ids}")
    @Operation(summary = "根据id删除人员群组管理明细", description = "删除人员群组管理明细,多个id用英文逗号分隔", method = "DELETE")
    public Result<EquipmentPersonGroupDetail> remove(@PathVariable(name = "ids", required = true) String ids) {
        String[] id = ids.split(",");
        for(String key : id){
            equipmentPersonGroupDetailService.delete(EquipmentPersonGroupDetail.class, key);
        }
        return Result.OK();
    }


}
